//Documentation complete
/**
 * @author Alex
 * @version 2009MR13
 */

package broker;

import problemDomain.ErrorBean;
import static problemDomain.ErrorBean.*;

import java.awt.geom.Point2D;
import java.sql.*;
import java.util.Date;
import java.util.Vector;

/**
 * Broker for generating a productivity vs. popularity report.
 */
public class ProductivityPopularityReportBroker extends ReportBroker {
	
	/* Singleton Constructor */
	
	private static ProductivityPopularityReportBroker broker;
	
	/**
	 * Default constructor. Calls the openConnection method.
	 * @throws ErrorBean
	 */
	private ProductivityPopularityReportBroker () throws ErrorBean {
		reportSQL = null;
	}
	
	/**
	 * Retrieves the static FoodItemBroker instance.
	 * @return static FoodItemBroker
	 * @throws ErrorBean
	 */
	public synchronized static ProductivityPopularityReportBroker getBroker () throws ErrorBean {
		if (broker == null)
			broker = new ProductivityPopularityReportBroker();
		return broker;
	}
	
	/* END Singleton Constructor */
	
	
	/* Prepared Statements */
	
	private PreparedStatement reportSQL;
	private PreparedStatement getReportSQL () throws ErrorBean {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT food_item_id, SUM(total_sold) AS num, SUM(extend) AS extnd FROM ");
		sb.append("(SELECT food_item_id, SUM(numsold) AS total_sold, price * SUM(numsold) AS extend ");
		sb.append("FROM v_menu_items_sold WHERE date_sold >= ? AND date_sold <= ? ");
		sb.append("GROUP BY menu_item_id) AS t_inner GROUP BY food_item_id");
		return prepare(reportSQL, sb.toString());
	}
	
	/* END Prepared Statements */
	
	
	/* Methods */
	
	/**
	 * Returns a set of data points which represent the data in the report.
	 * @param fromDate the <code>Date</code> which starts the date range of the report 
	 * @param toDate the <code>Date</code> which ends the date range of the report
	 * @return the <code>Point2D.Double</code> array holding the report data points
	 * @throws ErrorBean
	 */
	public Point2D.Double [] getDataPoints (Date fromDate, Date toDate) throws ErrorBean {
		PreparedStatement reportSQL = getReportSQL();
		try {
			reportSQL.setDate(1, new java.sql.Date(fromDate.getTime()));
			reportSQL.setDate(2, new java.sql.Date(toDate.getTime()));
			ResultSet results = reportSQL.executeQuery();
			
			Vector<Long> ids = new Vector<Long>();
			Vector<Point2D.Double> vec = new Vector<Point2D.Double>();
			while (results.next()) {
				try {
					Long id = new Long(results.getLong(1));
					Point2D.Double point = new Point2D.Double(results.getDouble(2), results.getDouble(3));
					ids.add(id);
					vec.add(point);
				} catch (SQLException e) {
					//bad data point
				}
			}
			
			Point2D.Double [] array = new Point2D.Double[vec.size()];
			for (int i = 0; i < array.length; i++)
				array[i] = vec.get(i);
			
			return array;
		} catch (SQLException e) {
			throwError(
				ErrorCode.BROKER_PREPARED_STATEMENTS_NOT_AVAILABLE,
				e.getMessage(),
				this);
			return null;
		}
	}
	
	/* END Methods */
	
}
